<?php

require_once 'SQLConnector.php';
class Account {
    public $accName;
    public $password;
    public $empStatus;

    public static function find($input) {
        global $pdoconn;
        if (empty($input)) {
//             echo 'right';
            $state = $pdoconn -> prepare("SELECT accName, password, empStatus FROM Account JOIN Employees ON Account.empID = Employees.empID");
            $state -> execute();
            return $state -> fetchAll(PDO::FETCH_OBJ);

        } else if ($input[':accName']){
            $state = $pdoconn -> prepare("SELECT accName, password, empStatus FROM Account JOIN Employees ON Account.empID = Employees.empID WHERE accName = :accName");
            $state -> execute(array(':accName' => $input[':accName']));
            return $state->fetchAll(PDO::FETCH_CLASS, "Account");
        } else {
            die("the database exploded");
            echo 'exploded db';
        }
    }

    public static function create($input){
        global $pdoconn;

        if (!empty($input)){
            $pdoconn -> beginTransaction();
            $pdoconn -> query("INSERT INTO Employees SET empFname = NULL");
            $st1 = $pdoconn -> prepare("SELECT MAX(empID) AS emp FROM Employees");
            $st1 -> execute();
            $result = $st1 -> fetchColumn();

            $st = $pdoconn -> prepare("INSERT INTO Account VALUES(:empID, :accName, :password)");
            $st -> execute(array(
                ':empID' => $result,
                ':accName' => $input[':accName'],
                ':password' => $input[':password']
            ));
            $pdoconn -> commit();
            return $st -> rowCount();
        } else {
            return 0;
        }
    }

    public static function update($input){
        global $pdoconn;
        if (!empty($input)) {
            $pdoconn -> beginTransaction();
            $st = $pdoconn -> prepare("UPDATE Account SET password = :password WHERE accName = :accName");
            $st -> execute($input);
            $pdoconn -> commit();
            return $st -> rowCount();
        } else {
            return 0;
        }
    }

    public static function delete($input){
        global $pdoconn;

        if (!empty($input)) {
            $pdoconn -> beginTransaction();
            $st = $pdoconn -> prepare("DELETE FROM Account WHERE accName = :accName ");
            $st -> execute($input);
            $pdoconn -> commit();
            return $st ->rowCount();
        } else {
            return 0;
        }
    }
}

